package com.cskaoyan.controller.admin.goods;

import com.cskaoyan.anno.ValidAnno;
import com.cskaoyan.bean.MarketGoods;
import com.cskaoyan.bean.bo.admin.goods.CreatBO;
import com.cskaoyan.bean.bo.admin.goods.UpdeleteGoodsBO;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.admin.goods.CatAndBrandVO;
import com.cskaoyan.bean.vo.admin.goods.GoodsDetailVo;
import com.cskaoyan.service.admin.goods.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author Mr.Tang
 * @since 2022/09/06 22:35
 */
@RestController
@RequestMapping("admin/goods")
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    /**
     * 请求 URL: http://localhost:8083/admin/goods/list?page=1&limit=20&sort=add_time&order=desc
     * 商品管理界面显示所有商品
     * @param basePageInfo
     * @return
     */
    @RequestMapping("list")
    public BaseRespVo list(BasePageInfo basePageInfo,Integer goodsId,Integer goodsSn,String name) {


        CommonData commonData = goodsService.selectGoodsInfo(basePageInfo,goodsId,goodsSn,name);
        return BaseRespVo.ok(commonData);
    }

    /**
     * 商品添加显示界面
     * @return
     */
    @RequestMapping("catAndBrand")
    public BaseRespVo catAndBrand() {
          CatAndBrandVO catAndBrandVO = goodsService.selectCateAndBrand();
          return BaseRespVo.ok(catAndBrandVO);
    }

    /**
     * 商品上架
     *
     *
     */
    @RequestMapping("create")
    @ValidAnno
    public BaseRespVo create(@RequestBody@Valid CreatBO creatBO, BindingResult bindingResult) {
         goodsService.insertCreatBO(creatBO);
     return BaseRespVo.ok(null);
    }

    /**
     * 删除商品
     * @param marketGoods
     * @return
     */
    @RequestMapping("delete")
    public BaseRespVo deleteForGoods(@RequestBody MarketGoods marketGoods) {
        goodsService.deleteForGoods(marketGoods);
        return BaseRespVo.ok(null);
    }


    /**
     * @description: TODO  显示商品细节
     * 请求URL： http://182.92.235.201:8083/admin/goods/detail?id=1181046
     * @date: 2022/9/8 23:11
     * @param: [goods]
     */
    @RequestMapping("detail")
    public BaseRespVo detail(Integer id) {
        GoodsDetailVo detailVo = goodsService.detail(id);
        return BaseRespVo.ok(detailVo);
    }

    /**
     * 更新商品
     * @param updeleteGoodsBO
     * @return
     */
    @RequestMapping("update")
    public BaseRespVo updateForGoods(@RequestBody UpdeleteGoodsBO updeleteGoodsBO) {
        goodsService.updateForGoods(updeleteGoodsBO);
         return BaseRespVo.ok(null);
    }
}
